<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加试听课程：</title>
    <link rel="stylesheet" type="text/css" href="../../plugins/layui/css/layui.css"/>
    <link href="../../kitAdmin/css/app.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="../../plugins/select2/dist/css/select2.min.css"/>
    <style type="text/css">
        /* .select2-container{
            display:none;
        } */

        .layui-unselect.layui-form-select {
            display: none;
        }
    </style>
</head>

<body>
<div class='kit-table'>
    <div style='margin:10px'>

        <form class='layui-form' action='' lay-filter='form-edit'>

            <div class='layui-inline'>
                <label class='layui-form-label'>选择老师</label>
                <select placeholder='' data-link='{:teacherId}' id='_teacherId' name='teacherId' class='layui-input'
                        style="width: 250px">
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <br>
            <div class='layui-inline' onclick="func()">
                <label class='layui-form-label' >选择学生</label>
                <select data-link='{:studentId}' id='_studentId' name='studentId' class='layui-input'
                        style="width: 250px" >
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <br>
            <div class='layui-inline'>
                <label class='layui-form-label'>课程</label>
                <select lay-filter="_course" data-link='{:course}' id='_course' name='course' class='layui-input'>
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <div class='layui-inline'>
                <label class='layui-form-label'>课程等级</label>
                <select lay-filter="_courseLevel" data-link='{:courseLevel}' id='_courseLevel' name='courseLevel'
                        class='layui-input'>
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <div class='layui-inline'>
                <label class='layui-form-label'>负责人</label>
                <select type='text' data-link='{:fuzerenId}' id='_fuzerenId' name='fuzerenId' class='layui-input'>
                    <option value='请选择' selected="true">请选择</option>
                </select>
            </div>
            <div class='layui-inline'>
                <label class='layui-form-label'>试听类型</label>
                <div class='layui-input-block'>
                    <!-- <input type='text' placeholder=''data-link='{:homeworkLike}' id='_homeworkLike' name='homeworkLike' class='layui-input'> -->
                    <input type="radio" data-link='{:trylistenType}' name="trylistenType" value="0" title="新试听"/>
                    <input type="radio" data-link='{:trylistenType}' name="trylistenType" value="1" title="二次试听"/>
                </div>
            </div>
            <div class='layui-inline'>
                <label class="layui-form-label">试听时间</label>
                <div class="layui-input-inline" style="margin-right: 0px; width: 146px;">
                    <input type='text' placeholder='' data-link='{:auditionTime}' id='_auditionTime' name='auditionTime'
                           class='layui-input'>
                </div>
                <span class="input-group-btn">
			        <a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button"
                       onclick="setAuditionTime()">添加</a>
		      	</span>
            </div>
            <div class='layui-inline'>
                <label class="layui-form-label">预留时间</label>
                <div class="layui-input-inline" style="margin-right: 0px; width: 146px;">
                    <input type='text' placeholder='' data-link='{:preTime}' id='_preTime' name='preTime'
                           class='layui-input'>
                </div>
                <span class="input-group-btn">
			        <a style="height: 38px;line-height:38px;" class="layui-btn layui-btn-sm" type="button"
                       onclick="setRetentionTime()">添加</a>
		      	</span>
            </div>
        </form>
    </div>
</div>
<script src="../../common/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="../../static/js/base.js" type="text/javascript" charset="utf-8"></script>
<script src="../../plugins/jsviews/jsviews.js" type="text/javascript"></script>
<script src="../../plugins/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="../../plugins/select2/dist/js/select2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    // 			var uid = getQueryVariable('studentId');
    var coursetemp = "1";
    var entity;

    var callback_save = function () {
        console.log($('.layui-form').serialize());
        return $('.layui-form').serialize();
    }


    function formatState(state) {
        alert("ok1");

        if (!state.id) return state.text;
        alert("ok");

        // if (!state.id && state.id=='请选择') {
        //     return '请选择' ;
        // }
        // return state.stuid + '   ' + state.text + '   ' + state.zoomId+ '   '+state.phone ;
        // return  state.phone ;
        var $state = $(state.text);
        return $state;
    };

    $(document).ready(function () {

        $("#_studentId").select2({
            ajax: {
                type: "get",
                url: baseUrl + '/students/select4',
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        key: params.term,
                    };
                },
                processResults: function (data) {
                    return {
                        results: data
                    };
                },
                cache: true
            },
            escapeMarkup: function (markup) {
                return markup;
            },
            minimumInputLength: 1,
            templateResult: function (data, container) {
                var $result = $("<span></span>");
                $result.text(data.showline);
                return $result;
            }
        });


        $("#_teacherId").select2({
            ajax: {
                type: "get",
                url: baseUrl + '/teachers/select3',
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        key: params.term,
                    };
                },
                processResults: function (data) {
                    return {
                        results: data
                    };
                },
                cache: true
            },
            escapeMarkup: function (markup) {
                return markup;
            },
            minimumInputLength: 1,
            templateResult: function (data, container) {
                var $result = $("<span></span>");
                $result.text(data.showline);
                return $result;
            }
        });

        // bindTeacherSelect2();
        bindFuzerenSelect2();
        bindLessonSelect2();
        bindLessonLevelSelect2();
        // bindStudentSelect2();
        // 2018-10-5 chenbin: 新增，绑定学生

        //select2通过模板绑定值以后不会自动刷新，需手动刷新一下；
        //注意，layui的select和select2用的不是一个控件，如果使用layui的select下面的这句可以注释掉
        //如果是select2，这下面的需要保留
        // $('select').select2().change();

        $('#_course').on("select2:select", function () {
            $('#_courseLevel').empty();
            $.ajax({
                url: baseUrl + "/levels/select2/" + $(this).val(),
                async: false,
                dataType: 'json',
                success: function (result) {
                    $('#_courseLevel').select2({
                        data: result
                    });
                }
            });
        });
        layui.use(['form'], function () {
            /* var form = layui.form;
            form.on('submit',function(){

            }) */
        });

        $("select#_studentId").change(function () {
            var stid = $(this).val();
            $.ajax({
                type: "get",
                url: baseUrl + '/students/' + stid,
                async: true,
                success: function (data) {
                    if (data.data != null && data.data.fuzerenId != null) {
                        select2ByValue(data.data.fuzerenId);
                    } else {
                        select2ByValue('请选择');
                    }
                    // $("#_studentId option[value="\'+ data[i].levelId + \'"]").remove();
                    // $("#_studentId").append("<option value=stid>Text</option>");
                    // $("#_studentId").select2().val(stid).trigger("change");
                }
            });
        });



    });


    function func(){

    }

    // 通过value 设置select2的默认值
    function select2ByValue(value) {
        var select2Obj = $("#_fuzerenId").select2();
        select2Obj.val(value).trigger("change");
    }

    function bindLessonLevelSelect2(data) {
        //console.log(entity);
        var courceData = [];
        $.ajax({
            type: "get",
            url: baseUrl + '/levels/select2/' + coursetemp,
            async: false,
            success: function (data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id: '请选择',
                    text: '请选择'
                });
            }
        });

        $('#_courseLevel').select2({
            data: courceData
        });
    }

    function bindLessonSelect2() {
        var courceData = [];
        $.ajax({
            type: "get",
            url: baseUrl + '/lessons/select2',
            async: false,
            success: function (data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id: '请选择',
                    text: '请选择'
                });
            }
        });

        $('#_course').select2({
            data: courceData
        });
    }


    function bindEnglishAbility() {
        var functional = entity.englishAbility;
        var groupCheckbox = $("input[name='englishAbility']");
        for (i = 0; i < groupCheckbox.length; i++) {
            var val = groupCheckbox[i].value;
            if (functional.split(',').indexOf(val) != -1) {
                groupCheckbox[i].checked = true;
            }
        }
    }


    function formatState(state) {
        return state.id + '   ' + state.text + '   ' + state.zoomId+ '   '+state.phone ;
    };

    function bindFuzerenSelect2() {
        var courceData = [];
        $.ajax({
            type: "get",
            url: baseUrl + '/admins/select2',
            async: false,
            success: function (data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id: '请选择',
                    text: '请选择'
                });
            }
        });

        $('#_fuzerenId').select2({
            data: courceData
        });
    }

    function bindTeacherSelect2() {
        var courceData = [];
        $.ajax({
            type: "get",
            url: baseUrl + '/teachers/select2',
            async: false,
            success: function (data) {
                //console.log(data);
                courceData = data;
                courceData.unshift({
                    id: '请选择',
                    text: '请选择'
                });
            }
        });

        $('#_teacherId').select2({
            data: courceData
        });
    }

    // 2018-9-24 chenbin: 选择试听时间
    function setAuditionTime() {
        var teacherid = $("#_teacherId").val();
        if (teacherid == "请选择") {
            alert("请选择老师");
            return;
        }
        var studentid = $("#_studentId").val();
        if (studentid == "请选择") {
            alert("请选择学生");
            return;
        }
        var studentName = $("#_studentId").find("option:selected").text(); // 2018-10-7 chenbin: 获取学员名
// 				var studentid=uid;
// 				console.log(uid);
        parent.layer.open({
            id: teacherid + 'info',
            maxmin: true,
            type: 2,
            area: modalArea,
            content: './views/Period_Used.html?techerid=' + teacherid + '&studentid=' + studentid + '&studentName=' + studentName + '&type=audition',
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                console.log(data);
                $.ajax({
                    url: baseUrl + '/teacherPeriods/periodUsed/save/' + data[0] + '&' + data[2],
                    type: 'POST',
                    data: data[1],//teacherId:'1245745'
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        $("#_auditionTime").val(data.data);
                        parent.layer.close(index);
                    }
                });
            },
            btn2: function (index, layero) {
                //按钮【按钮二】的回调
                console.log(layero);
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    }

    // 2018-9-24 chenbin: 选择预留时间
    function setRetentionTime() {
        var teacherid = $("#_teacherId").val();
        if (teacherid == "请选择") {
            alert("请选择老师");
            return;
        }
        var studentid = $("#_studentId").val();
        if (studentid == "请选择") {
            alert("请选择学生");
            return;
        }
        var studentName = $("#_studentId").find("option:selected").text(); // 2018-10-7 chenbin: 获取学员名
// 				var studentid=uid;
// 				console.log(uid);
        parent.layer.open({
            id: teacherid + 'info',
            maxmin: true,
            type: 2,
            area: modalArea,
            content: './views/Period_Formal.html?techerid=' + teacherid + '&studentid=' + studentid + '&studentName=',
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                console.log(layero);
                $.ajax({
                    url: baseUrl + '/teacherPeriods/periodUsedFormal/save/' + data[0] + '&' + data[2],
                    type: 'POST',
                    data: data[1],//teacherId:'1245745'
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        $("#_preTime").val(data.data);
                        parent.layer.close(index);
                    }
                });
            },
            btn2: function (index, layero) {
                //按钮【按钮二】的回调
                console.log(layero);
                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
    }


</script>
</body>

</html>
